#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
using namespace std;
int main()
{
  pid_t id=fork();
  int status=0;
  if(id==0)
  {
    char* ch[]={
      "ls",
      "-al",
      nullptr
    };
    execvpe("ls",ch);
    
   exit(100);
  }
  else{
    while(1)
    {
     int rid=waitpid(id,&status,WNOHANG);
     if(rid>0)
     {
       cout<<"子进程运行结束"<<endl;
       if(WIFEXITED(status))
       {
         cout<<"子进程正常退出 "<<"退出码："<<WEXITSTATUS(status)<<endl;
       }
       else cout<<"子进程异常退出！"<<endl;
       break;
     }
     else if(rid==0)
     {
       cout<<"子进程尚未结束！"<<endl;
     }
     else{
       cout<<"回收失败！";
     }
     sleep(1);
    }
  }
  return 0;
}
